package com.androidian.tippit;


import com.androidian.tippit.R;
import com.androidian.tippit.fragments.FragmentMenu;
import com.androidian.tippit.utilities.Callbacks.StandardVariables;
import com.androidian.tippit.utilities.Callbacks.WizardLocker;
import com.slidingmenu.lib.SlidingMenu;
import com.viewpagerindicator.LinePageIndicator;


import android.os.Bundle;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class ActivityHome extends FragmentActivity implements WizardLocker, StandardVariables
{

	private ViewPager mViewPager;
	
	private LinePageIndicator mIndicator;
	
	private Button mPreviousButton;
	private Button mNextButton;
	
	private FragmentManager mFragmentManager; 
	
	public double billTotal;
	public double billWithTipTotal;
	public double tipTotal;
	public double billPerPerson;
	
	public int tipPercentage = 15;
	public int peopleNumber = 1;
	
	public SlidingMenu mSlidingMenu;
	
	
	@Override
	protected void onCreate(Bundle mBundle) 
	{
		
		super.onCreate(mBundle);
		setContentView(R.layout.activity_home);
		
			if(mBundle != null)
			{
				
				billTotal = mBundle.getDouble("billTotal", billTotal);
				billWithTipTotal = mBundle.getDouble("billWithTipTotal", billWithTipTotal);
				tipTotal = mBundle.getDouble("tipTotal", tipTotal);
				billPerPerson = mBundle.getDouble("billPerPerson", billPerPerson);
				
				tipPercentage = mBundle.getInt("tipPercentage", tipPercentage);
				peopleNumber = mBundle.getInt("peopleNumber", peopleNumber);
				
			}
		  		
		final HomePagerAdapter mHomePagerAdapter = new HomePagerAdapter
	    		   (getSupportFragmentManager());
		
	    final ActionBar mActionBar = getActionBar();
		  
	    mActionBar.setDisplayShowHomeEnabled(true); 
	    
		mFragmentManager = getFragmentManager();

	    
	    mViewPager = (ViewPager)
				findViewById(R.id.pager);
	    
		mViewPager.setAdapter(mHomePagerAdapter);
	    

	    mSlidingMenu = new SlidingMenu
				(ActivityHome.this);
	    
		mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		mSlidingMenu.setBehindOffset(250);
		mSlidingMenu.setFadeDegree(0.35f);
		mSlidingMenu.setShadowDrawable(getResources().getDrawable(R.drawable.shape_menushadow));
		mSlidingMenu.setShadowWidth(16);

			if (findViewById(R.id.slidingmenu_frame) == null) 
			{

				mSlidingMenu.setMenu(R.layout.menu_frame);
				mSlidingMenu.setSlidingEnabled(true);
				mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
				
			    mActionBar.setDisplayHomeAsUpEnabled(true);        
			    mActionBar.setHomeButtonEnabled(true);
			    
			} 
			
			else 
			{

				View dummyView = new View(this);
				mSlidingMenu.setMenu(dummyView);			
				mSlidingMenu.setSlidingEnabled(false);
				mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
				
			    mActionBar.setDisplayHomeAsUpEnabled(false);        
			    mActionBar.setHomeButtonEnabled(false);
				
			}
							
	    mFragmentManager.beginTransaction().replace
			(R.id.slidingmenu_frame, new FragmentMenu()).commit();
			
		//TODO Jake Wharton code begins here
		mIndicator = (LinePageIndicator)
				 findViewById(R.id.indicator);
		mIndicator.setWizardEnabled(true);
		mIndicator.setReviewPage(3);
		mIndicator.setReviewLineColor(getResources().getColor(R.color.holo_orange_dark));
		mIndicator.setPreviousLineColor(getResources().getColor(R.color.holo_blue_light));
		
		mIndicator.setOnPageChangeListener(new OnPageChangeListener()
		{

			@Override
			public void onPageScrollStateChanged(int arg0) 
			{

				// Not used
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) 
			{

				// Not used
				
			}

			@Override
			public void onPageSelected(int position) 
			{
				
				mIndicator.setPreviousCount(position);
				
			}
						
		});
		
		
        final float density = getResources().getDisplayMetrics().density;
        mIndicator.setSelectedColor(this.getResources().getColor(R.color.holoblue_dark));
        mIndicator.setUnselectedColor(0xFF888888);
        mIndicator.setStrokeWidth(3 * density);
        mIndicator.setLineWidth(30 * density);	
        mIndicator.setCentered(false);
        
        
	    mIndicator.setViewPager(mViewPager);
	    
		//TODO Jake Wharton code ends here

        
		mPreviousButton = (Button)
				findViewById(R.id.previousButton);
		
		mPreviousButton.setOnClickListener(new OnClickListener()
		{
		
			@Override
			public void onClick(View view) 
			{
		
				view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
				
				mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
				
			}
		
		});
		
		mNextButton = (Button)
				findViewById(R.id.nextButton);
		
		mNextButton.setOnClickListener(new OnClickListener()
		{
		
			@Override
			public void onClick(View view) 
			{
				
				view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

				mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
				
			}
		
		});
				
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		
	    switch (item.getItemId()) 
	    {
	    
	        case android.R.id.home:
	        	
		    	if(mSlidingMenu.isMenuShowing())
		    	{
		    		
		    		mSlidingMenu.showContent();
		    		
		    	}
		    	
		    	else
		    	{
		    		
			    	mSlidingMenu.showMenu();
		    	}
		    	
		        return true;
	            
	            
	        case R.id.preferences:

		        	final Intent mIntent = new Intent(ActivityHome.this, 
		        			PreferenceActivity.class);
		        	startActivity(mIntent);
					
	            return true;
	            
	            
	        default:
	        	
	            return super.onOptionsItemSelected(item);
	            
	    }
	    
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		 
	    final MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    
	    return true;
	    
	}
	

	
	
	
	
	@Override
	public void onBackPressed()
	{
		
		if(mFragmentManager.getBackStackEntryCount() > 0)
		{
			
			mFragmentManager.popBackStack();
			
		}
		
		else
		{
			
			super.onBackPressed();
			
		}
		
	}


	@Override
	public void lockNavigation(final boolean lockPreviousButton, final boolean lockNextButton, final boolean lockViewPager) 
	{

		if(lockViewPager)
		{
			
			mViewPager.setOnTouchListener(new OnTouchListener()
			{

				@Override
				public boolean onTouch(View v, MotionEvent event) 
				{

					// Dummy listener to override page swipe
					
					return true;
					
				}			
				
			});			
			
		}

		
		if(lockPreviousButton)
		{
			
			mPreviousButton.setTextColor(getResources().getColor(R.color.divider_grey));
			mPreviousButton.setClickable(false);
			mPreviousButton.setFocusable(false);
			
		}
		
		
		if(lockNextButton)
		{
			
			mNextButton.setTextColor(getResources().getColor(R.color.divider_grey));
			mNextButton.setClickable(false);
			mNextButton.setFocusable(false);
			
		}
		
	}

	
	@Override
	public void unlockNavigation(final boolean unlockPreviousButton, final boolean unlockNextButton, final boolean unlockViewPager) 
	{
		
		if(unlockViewPager)
		{
			
			mViewPager.setOnTouchListener(null);

		}

		
		if(unlockPreviousButton)
		{
			
			mPreviousButton.setVisibility(View.VISIBLE); 
			mPreviousButton.setTextColor(getResources().getColor(R.color.black));
			mPreviousButton.setClickable(true);
			mPreviousButton.setFocusable(true);
			
		}
		
		
		if(unlockNextButton)
		{
			
			mNextButton.setVisibility(View.VISIBLE); 
			mNextButton.setTextColor(getResources().getColor(R.color.black));
			mNextButton.setClickable(true);
			mNextButton.setFocusable(true);
			
		}
	
	}


	@Override
	public void billTotalSet(double billTotal) 
	{

		this.billTotal = billTotal;
		
	}

	@Override
	public void tipPercentageSet(int tipPercentage) 
	{
		
		this.tipPercentage = tipPercentage;
		
	}

	@Override
	public void peopleNumberSet(int peopleNumber) 
	{

		this.peopleNumber = peopleNumber;

	}


	@Override
	public void calculateTotals() 
	{

		tipTotal = billTotal * (tipPercentage * .01);
		
		billWithTipTotal = billTotal + tipTotal;
		
		billPerPerson = billWithTipTotal / peopleNumber;
				
	}
	

	@Override
	public void onSaveInstanceState(Bundle mBundle) 
	{
		
	  super.onSaveInstanceState(mBundle);
	  	  
	  mBundle.putDouble("billTotal", billTotal);
	  mBundle.putDouble("billWithTipTotal", billWithTipTotal);
	  mBundle.putDouble("tipTotal", tipTotal);
	  mBundle.putDouble("billPerPerson", billPerPerson);

	  mBundle.putInt("tipPercentage", tipPercentage);
	  mBundle.putInt("peopleNumber", peopleNumber);

	}

	



}
